package logic.concurrent;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UseForConcurrentHashMap {

//两步操作	
//1-putIfAbsent
//2-removeIfEqual	//状态
//3-replaceIfEqual	
private static ConcurrentHashMap<String, Long> lockMap=new ConcurrentHashMap<String, Long>();



@SuppressWarnings("unused")
/**由虚拟机内存维护的ConcurrentHashMap并发锁
 * ++定时释放
 * @param lockStr
 * @return
 */
public static boolean getlock(String lockStr){
	Long flag=lockMap.putIfAbsent(lockStr, System.currentTimeMillis());
	if(null!=flag)return false;
	return true;
}
/**timer..
 * 定时器/新线程执行
 */
public static void freeAll(long Hour){
	long time=System.currentTimeMillis();
	for(Map.Entry<String,Long> entry:lockMap.entrySet()){
		if(entry.getValue()<time-Hour*3600)
			lockMap.remove(entry);
	}
}


//仅当K被映射到V时才移除,不满足条件返回false
// boolean remove(K key,V value);

//仅当K被映射到oldValue时才替换为newValue,不满足条件返回false
//boolean replace(K key,V oldValue, V newValue);

//仅当K存在映射时才替换
//V replace(K key, V newValue)





@SuppressWarnings("unused")
public static void main(String[] args) {
//	Long str=lockMap.put("abb", 111l);
//	lockMap.put("222", 222l);
//	lockMap.put("223", 222l);
//	lockMap.put("224", 222l);
//	System.out.println(lockMap.size());
//	for(Map.Entry<String, Long> entry:lockMap.entrySet()){
//		if("222".equals(entry.getKey())){
//			lockMap.remove(entry.getKey());
//		}
//	}
//	System.out.println(lockMap);
//	System.out.println(lockMap.size());
	FooThread foo=new FooThread("线程1", "1111111");
	FooThread foo1=new FooThread("线程2", "1111112");
	FooThread foo2=new FooThread("线程3", "1111111");
	FooThread foo3=new FooThread("线程4", "1111113");
	FooThread foo4=new FooThread("线程5", "1111113");
	
	foo.start();
	foo1.start();
	foo2.start();
	foo3.start();
	foo4.start();
}









	
	
	
	
	
	
	
	
	
}
